#include "CSensor.h"
#include "CHardware.h"
#include <msclr/gcroot.h>
#using "OpenHardwareMonitorLib.dll"

using namespace msclr;
using namespace System::Runtime::InteropServices;

namespace NOpenHardwareMonitor {

namespace NHardware {


class CSensorPrivate
{
public:
	CHardware* pHardware = nullptr;
	gcroot<OpenHardwareMonitor::Hardware::ISensor^> mSensor;
};


CSensor::CSensor(CHardware* Hardware, const qint32& Index)
	: QObject(Hardware)
	, d_ptr(new CSensorPrivate())
{
	Q_D(CSensor);
	d->pHardware = Hardware;
	d->mSensor = (*(gcroot<OpenHardwareMonitor::Hardware::IHardware^>*)d->pHardware->GetHandle())->Sensors[Index];
}

CSensor::~CSensor()
{  }

void CSensor::Release()
{ delete this; }

CSensor::EType CSensor::GetType() const
{ Q_D(const CSensor); return (CSensor::EType)(qint32)d->mSensor->SensorType; }

void CSensor::ResetMin()
{ Q_D(CSensor); d->mSensor->ResetMin(); }

void CSensor::ResetMax()
{ Q_D(CSensor); d->mSensor->ResetMax(); }

qint32 CSensor::GetHashCode() const
{ Q_D(const CSensor); return d->mSensor->GetHashCode(); }

QString CSensor::GetIdentifier() const
{
	Q_D(const CSensor);

	char* m_String = (char*)(void*)Marshal::StringToHGlobalAnsi(d->mSensor->Identifier->ToString());
	return QString(m_String);
}

qint32 CSensor::GetIndex() const
{ Q_D(const CSensor); return d->mSensor->Index; }

bool CSensor::IsDefaultHidden() const
{ Q_D(const CSensor); return d->mSensor->IsDefaultHidden; }

float CSensor::GetMax() const
{ Q_D(const CSensor); return (float)d->mSensor->Max; }

float CSensor::GetMin() const 
{ Q_D(const CSensor); return (float)d->mSensor->Min; }

QString CSensor::GetName() const
{
	Q_D(const CSensor);

	char* m_String = (char*)(void*)Marshal::StringToHGlobalAnsi(d->mSensor->Name);
	return QString(m_String);
}

float CSensor::GetValue() const
{ Q_D(const CSensor); return (float)d->mSensor->Value; }


}}